﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SceneFrame
{
    /// <summary>
    /// 场景
    /// </summary>
    public class Scene
    {
        public Scene()
        {
            Name = GetType().Name;
        }

        public Scene(Dictionary<string, string> context)
            : this()
        {
            ContextData = context ?? new Dictionary<string, string>();
        }

        /// <summary>
        /// 场景的标识
        /// </summary>
        public virtual long Id { get; set; }

        /// <summary>
        /// 场景的名称（仅展示用）
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        /// 过滤器
        /// </summary>
        public virtual List<IFilter> Filters { get; set; } = new List<IFilter>();

        /// <summary>
        /// 继承上下文
        /// </summary>
        /// <param name="scene"></param>
        public virtual void Inherit(Scene scene)
        {
            ContextData = scene.GetContextData();
        }

        /// <summary>
        /// 获取场景数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual T GetSceneData<T>(string key)
            where T : class
        {
            return default(T);
        }

        /// <summary>
        /// 场景数据
        /// </summary>
        protected virtual Dictionary<string, string> ContextData { get; set; } = new Dictionary<string, string>();

        /// <summary>
        /// 获取场景数据
        /// </summary>
        /// <returns></returns>
        public virtual Dictionary<string, string> GetContextData()
        {
            lock (this)
                return new Dictionary<string, string>(ContextData);
        }


        /// <summary>
        /// 获取场景数据
        /// </summary>
        /// <returns></returns>
        public virtual string GetContextData(string key)
        {
            lock (this)
                return ContextData.ContainsKey(key) ? ContextData[key] : string.Empty;
        }

        /// <summary>
        /// 更新场景数据
        /// </summary>
        /// <param name=""></param>
        /// <param name=""></param>
        public virtual void UpdateSceneData(Trigger trigger)
        {
            foreach (var item in trigger.AllKeys)
                ContextData[item] = trigger.GetData(item);
        }

        /// <summary>
        /// 尝试触发该场景
        /// </summary>
        /// <param name="trigger"></param>
        public void Trigger(Trigger trigger)
        {
            foreach (var item in Filters ?? new List<IFilter>())
                item.Scene = this;

            // 判定是否需要触发
            if (!(Filters?.All(n => n.Filter(trigger)) ?? true))
                return;

            lock (this)
            {
                // 更新数据
                UpdateSceneData(trigger);

                // 让过滤器也参加到数据更新中来
                foreach (var item in Filters ?? new List<IFilter>())
                    item.UpdateContextData(ContextData);
            }

            // 触发后续逻辑
            OnTrigger();
        }

        /// <summary>
        /// 被触发的函数
        /// </summary>
        public virtual void OnTrigger()
        {

        }
    }
}
